//package com.example.caorl.job1to4.gateway.outbound.netty4;
//
//import com.example.caorl.job1to4.gateway.filter.HttpRequestFilter;
//import com.example.caorl.job1to4.gateway.outbound.IOutboundHandler;
//import com.example.caorl.job1to4.gateway.router.HttpEndpointRouter;
//import com.example.caorl.job1to4.gateway.router.RouterContext;
//import io.netty.bootstrap.Bootstrap;
//import io.netty.buffer.Unpooled;
//import io.netty.channel.*;
//import io.netty.channel.nio.NioEventLoopGroup;
//import io.netty.channel.socket.SocketChannel;
//import io.netty.channel.socket.nio.NioSocketChannel;
//import io.netty.handler.codec.http.*;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.net.URI;
//import java.util.ArrayList;
//import java.util.List;
//
//public class NettyHttpClient implements IOutboundHandler {
//
//    private static Logger logger = LoggerFactory.getLogger(NettyHttpClient.class);
//
//    private final List<String> proxyServer;
////    private HttpOutboundHandler outBoundHandler;
//    //    private HttpRequestFilter reqFilter = new HeaderHttpRequestFilter();
////    private HttpRequestFilter reqFilter;
//
//    HttpEndpointRouter router = RouterContext.getStrategy(System.getProperty(RouterContext.STRATEGY_KEY));
//
//
//    public NettyHttpClient(List<String> proxyServer) {
//        this.proxyServer = proxyServer;
////        this.reqFilter = httpRequestFilter;
////        this.outBoundHandler = new HttpOutboundHandler(this.proxyServer);
//    }
//
//    @Override
//    public void handle(FullHttpRequest fullRequest1, ChannelHandlerContext ctx1, HttpRequestFilter reqFilter) throws Exception {
//        logger.info("this is nettyClient--------------");
//        EventLoopGroup workerGroup = new NioEventLoopGroup();
//
//        try {
//            Bootstrap b = new Bootstrap();
//            b.group(workerGroup);
//            b.channel(NioSocketChannel.class);
//            b.option(ChannelOption.SO_KEEPALIVE, true);
//            b.handler(new ChannelInitializer<SocketChannel>() {
//                @Override
//                public void initChannel(SocketChannel ch) throws Exception {
//                    // 客户端接收到的是httpResponse响应，所以要使用HttpResponseDecoder进行解码
//                    ch.pipeline().addLast(new HttpResponseDecoder());
//                    // 客户端发送的是httprequest，所以要使用HttpRequestEncoder进行编码
//                    ch.pipeline().addLast(new HttpRequestEncoder());
////                    ch.pipeline().addLast(new HttpClientOutboundHandler());
//                    ch.pipeline().addLast(new NettyHttpClientOutboundHandler());
//                }
//            });
//
//            // Start the client.
//            String routeUrl = router.route(proxyServer);
//            URI uri = new URI(routeUrl);
////            URI uri = new URI(routeUrl+fullRequest.uri());
//            String host = uri.getHost();
//            int port = uri.getPort();
//            ChannelFuture f = b.connect(host, port).sync();
////            reqFilter.filter(fullRequest, ctx);
//            String msg = "Are you ok?";
//            DefaultFullHttpRequest request = new DefaultFullHttpRequest(
//                    HttpVersion.HTTP_1_1, HttpMethod.POST, uri.toASCIIString(),
//                    Unpooled.wrappedBuffer(msg.getBytes()));
//            // 构建http请求
//            request.headers().set(HttpHeaderNames.HOST, host);
//            request.headers().set(HttpHeaderNames.CONNECTION,
//                    HttpHeaderNames.CONNECTION);
//            request.headers().set(HttpHeaderNames.CONTENT_LENGTH,
//                    request.content().readableBytes());
//            request.headers().set("messageType", "normal");
//            request.headers().set("businessType", "testServerState");
//            // 发送http请求
//            f.channel().write(request);
//            f.channel().flush();
//            ChannelFuture future = f.channel().closeFuture().sync();
//
//
//        } finally {
//            workerGroup.shutdownGracefully();
//        }
//
//    }
//
//    private String formatUrl(String backend) {
//        return backend.endsWith("/")?backend.substring(0,backend.length()-1):backend;
//    }
//
//    public static void main(String[] args) throws Exception {
//        List<String> url = new ArrayList<>();
//        url.add("http://127.0.0.1:8081");
//        new NettyHttpClient(url).handle(null,null,null);
//    }
//}
//
//
